package com.qin.mall.controller;

import com.qin.mall.api.CommonResult;
import com.qin.mall.dto.PmsBrandDto;
import com.qin.mall.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品品牌管理
 * @author Leisure
 * create 2022 2022/10/11 21:13
 **/
@Api(tags = "PmsBrandController", description = "商品品牌管理")
@Controller
@RequestMapping("/brand")
public class PmsBrandController {
    @Autowired
    private PmsBrandService brandService;
    
    @ApiOperation(value = "获取全部品牌信息")
    @RequestMapping(value = "/listAll",method = RequestMethod.GET)
    @ResponseBody
    public Object getBrandList(){
        //交给service处理并用CommonResut处理结果
        return new CommonResult().success(brandService.listAllBrand());
    }
    
    @ApiOperation(value = "添加品牌")
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    @ResponseBody
    //验证参数
    public Object createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, BindingResult result ){
        CommonResult commonResult;
        int count = brandService.createBrand(pmsBrand);
        //如果返回1 则添加成功,如果返回0 则添加失败
        if (count == 1) {
            //
            //commonResult = new CommonResult().success(pmsBrand);
            commonResult = new CommonResult().success(count);
        } else {
            commonResult = new CommonResult().failed();
        }
        //返回处理结果
        return commonResult;
    }
    
    @ApiOperation(value = "更新品牌")
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    @ResponseBody
    //携带id 对的品牌传进来
    public Object updateBrand(@PathVariable("id") Long id,
                              @Validated @RequestBody PmsBrandDto pmsBrandDto,
                              BindingResult result){
        CommonResult commonResult;
        //如果返回1 则更新成功 、如果返回0 则返回失败
        int count = brandService.updateBrand(id, pmsBrandDto);
        if (count == 1) {
            commonResult = new CommonResult().success(count);
        } else {
            commonResult = new CommonResult().failed();
        }
        return commonResult;
        
    }
    
    @ApiOperation(value = "通过id单个删除品牌")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    //删除指定id的品牌
    public Object deleteBrand (@PathVariable("id") Long id){
        int count = brandService.deleteBrand(id);
        //如果返回1 则删除成功 、如果返回0 则删除失败
        if (count == 1) {
            return new CommonResult().success(null);
        } else {
            return new CommonResult().failed();
        }
    }
    
    @ApiOperation(value = "分页查询品牌列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    //传入页面数和页面大小
    public Object listBrand(@RequestParam(value = "keyword", required = false) String keyword,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize){
        return new CommonResult().pageSuccess(brandService.listBrand(keyword,pageNum, pageSize));
    }
    
    @ApiOperation(value = "根据编号查询品牌信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Object getBrand(@PathVariable("id") Long id){
        return new CommonResult().success(brandService.getBrand(id));
        
    }

    @ApiOperation(value = "批量删除品牌")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    public Object deleteBatch(@RequestParam("ids") List<Long> ids) {
        int count = brandService.deleteBrand(ids);
        if (count > 0) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }

    @ApiOperation(value = "批量更新显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public Object updateShowStatus(@RequestParam("ids") List<Long> ids,
                                   @RequestParam("showStatus") Integer showStatus) {
        int count = brandService.updateShowStatus(ids, showStatus);
        if (count > 0) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }

    @ApiOperation(value = "批量更新厂家制造商状态")
    @RequestMapping(value = "/update/factoryStatus", method = RequestMethod.POST)
    @ResponseBody
    public Object updateFactoryStatus(@RequestParam("ids") List<Long> ids,
                                      @RequestParam("factoryStatus") Integer factoryStatus) {
        int count = brandService.updateFactoryStatus(ids, factoryStatus);
        if (count > 0) {
            return new CommonResult().success(count);
        } else {
            return new CommonResult().failed();
        }
    }
    
}
